package com.login;

/**
 * 实现电厂职工培训信息管理系统的登录功能，本实验连接Sql Server数据库，实现登录功能。
 */
import javax.swing.*;

import java.awt.*;

import javax.swing.JCheckBox;
import javax.swing.border.*;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import javax.swing.JPasswordField;

import com.dao.impl.UserDAOImpl;
import com.tool.WindowUtil;

@SuppressWarnings("serial")
public class LoginForm extends JFrame {
	@SuppressWarnings("unused")
	private JTextField textField_4;
	private JTextField textField;
	private JPasswordField passwordField;
	private JTextField textField_1;

	/**
	 * Create the frame.
	 */

	public LoginForm() {
		JPanel panel = new JPanel();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 869, 502);

		panel.setForeground(Color.CYAN);
		panel.setToolTipText("");
		panel.setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		panel.setBounds(457, 61, 253, 293);
		getContentPane().add(panel);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(248, 248, 255));
		panel_1.setForeground(Color.GREEN);
		panel_1.setBounds(569, 63, 247, 329);
		panel_1.setBorder(new CompoundBorder(new LineBorder(new Color(0, 255,
				255)), new MatteBorder(1, 1, 1, 1, (Color) new Color(0, 255,
				255))));
		panel.add(panel_1);
		panel_1.setLayout(null);

		JLabel Label_1 = new JLabel(new ImageIcon(
				"E:\\eclipse\\daimadaquan\\images\\6.png"));
		Label_1.setBounds(77, 10, 76, 33);
		panel_1.add(Label_1);
		Label_1.setVerticalAlignment(SwingConstants.BOTTOM);

		textField = new JTextField();
		textField.setBounds(101, 57, 111, 33);
		panel_1.add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(101, 112, 111, 33);
		panel_1.add(passwordField);

		JLabel Label_8 = new JLabel("验证码：");
		Label_8.setFont(new Font("楷体", Font.PLAIN, 20));
		Label_8.setBounds(3, 173, 88, 33);
		panel_1.add(Label_8);

		JLabel Label_3 = new JLabel(new ImageIcon(
				"E:\\eclipse\\plant training\\images\\5.png"));
		Label_3.setBounds(146, 173, 91, 33);
		panel_1.add(Label_3);
		Label_3.setVerticalAlignment(SwingConstants.BOTTOM);

		JLabel label_2 = new JLabel("用户名：");
		label_2.setFont(new Font("楷体", Font.PLAIN, 20));
		label_2.setBounds(3, 55, 88, 33);
		panel_1.add(label_2);

		JLabel label_1 = new JLabel("密码：");
		label_1.setFont(new Font("楷体", Font.PLAIN, 20));
		label_1.setBounds(3, 112, 88, 33);
		panel_1.add(label_1);

		JButton Button = new JButton("\u767B\u5F55");
		Button.setBounds(77, 286, 93, 33);
		panel_1.add(Button);
		Button.setFont(new Font("楷体", Font.PLAIN, 18));
		Button.setForeground(Color.CYAN);

		textField_1 = new JTextField();
		textField_1.setBounds(72, 173, 66, 33);
		panel_1.add(textField_1);
		textField_1.setColumns(10);
		
				JCheckBox CheckBox = new JCheckBox("记住登录名");
				CheckBox.setBounds(6, 227, 117, 23);
				panel_1.add(CheckBox);
				CheckBox.setFont(new Font("楷体", Font.PLAIN, 16));
		Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource() == Button) {
					do_login_action(e);
				}
			}
		});

		JLabel Label_head = new JLabel(new ImageIcon(
				"E:\\eclipse\\daimadaquan\\images\\7.png"));
		Label_head.setBounds(504, 0, 336, 23);
		Label_head.setForeground(Color.WHITE);
		Label_head.setVerticalAlignment(SwingConstants.BOTTOM);
		Label_head.setToolTipText("");
		Label_head.setText("");
		panel.add(Label_head);

		JLabel Label_7 = new JLabel("电厂职工培训信息管理系统");
		Label_7.setForeground(Color.CYAN);
		Label_7.setFont(new Font("楷体", Font.PLAIN, 30));
		Label_7.setBounds(121, 37, 368, 45);
		panel.add(Label_7);

		JLabel Label_4 = new JLabel(
				"Copyright © 2015-2016 Tencent. All Rights Reserved.    电厂职工培训管理系统      技术支持联系方式：0351-6998011");
		Label_4.setBounds(62, 417, 675, 23);
		panel.add(Label_4);

		JLabel Label_5 = new JLabel("版权所有 备案证号：晋ICP备050024564号");
		Label_5.setBounds(280, 438, 263, 23);
		panel.add(Label_5);
		JLabel Label_bg = new JLabel(new ImageIcon(
				"E:\\eclipse\\plant training\\images\\2.jpg"));
		Label_bg.setBounds(0, 0, 863, 475);
		panel.add(Label_bg);
		/**
		 * 设置UI的感观风格
		 */
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setLocation(WindowUtil.setLocation(this.getWidth(), this.getHeight()));
		//frame.setVisible(true);
	}

	protected void do_login_action(ActionEvent e) {
		String username = textField.getText().trim();
		String password = new String(passwordField.getPassword());

		if ("".equals(username) || "".equals(password)) {
			JOptionPane.showMessageDialog(null, "用户名或密码不能为空！");
		} else {
			UserDAOImpl dao = new UserDAOImpl();
			try {
				boolean flag = dao.find(username, password);

				if (flag) {
					LoginForm.this.setVisible(false);
					AddTrainingPlanForm addTrainingPlanForm=new AddTrainingPlanForm();
					addTrainingPlanForm.setVisible(true);;
				} else {
					JOptionPane.showMessageDialog(null, "用户名或密码有误！");
				}
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
}